package com.foreveross.action.weixin;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.HouseUserService;
import com.foreveross.service.ReservationService;
import com.foreveross.util.FileUtil;
import com.foreveross.util.JSONUtil;
import com.foreveross.vo.common.ResultVo;
import com.foreveross.weixin.common.Constant;
import com.foreveross.weixin.util.SignUtil;
import com.foreveross.weixin.util.WeixinUtil;
import com.foreveross.weixin.vo.AccessToken;
import com.foreveross.weixin.vo.JsApiTicket;
import com.foreveross.weixin.vo.SignVo;
import com.foreveross.weixin.vo.UserInfo;

@Controller
@RequestMapping(value = "/weixin/WeiXinController")
public class WeiXinController {

	private Logger log = Logger.getLogger(WeiXinController.class);

	@Resource
	private HouseUserService houseUserService;
	
	/**
	 * 验证用户是否是认证用户
	 * 
	 * @param request
	 * @param response
	 * @param weixinId
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/isAuthentication")
	public void isAuthentication(HttpServletRequest request,
			HttpServletResponse response, String weixinId) {
		ResultVo resultVo = new ResultVo();
		try {
			boolean	isAuthentication = houseUserService.isAuthentication(weixinId);
			Map map = new HashMap();
			if (isAuthentication) {
				map.put("code", "1");
			} else {
				resultVo.setMessage("该用户未通过认证!");
				map.put("code", "0");
			}
			resultVo.setSuccess(true);
			resultVo.setData(map);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("服务器查询异常");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 用户同意授权，获取code
	 * 
	 * @param request
	 * @param response
	 * @param redirectUrl
	 * @param state
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/validateOauth2AuthorizeCode")
	public void validateOauth2AuthorizeCode(HttpServletRequest request,
			HttpServletResponse response,String appId, String redirectUrl,boolean isBase, String state) {
		ResultVo resultVo = new ResultVo();
		try {
			if(appId == null || "".equals(appId)) appId = Constant.appId;
			String str = WeixinUtil.validateOauth2AuthorizeCode(appId,
					redirectUrl, isBase,state);
			resultVo.setSuccess(true);
			resultVo.setData(str);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("用户授权失败!");
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/getOauth2AccessToken")
	public void getOauth2AccessToken(HttpServletRequest request,
			HttpServletResponse response,String appId,String appSecret, String code) {
		ResultVo resultVo = new ResultVo();
		try {
			if(appId == null || "".equals(appId)) appId = Constant.appId;
			if(appSecret == null || "".equals(appSecret)) appSecret = Constant.appSecret;
			AccessToken accessToken = WeixinUtil.getOauth2AccessToken(
					appId, appSecret, code);
			resultVo.setSuccess(true);
			resultVo.setData(accessToken);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("获取Oauth2AccessToken失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/refreshOauth2AccessToken")
	public void refreshOauth2AccessToken(HttpServletRequest request,
			HttpServletResponse response,String appId, String refreshToken) {
		ResultVo resultVo = new ResultVo();
		try {
			if(appId == null || "".equals(appId)) appId = Constant.appId;
			AccessToken accessToken = WeixinUtil.refreshOauth2AccessToken(
					appId, refreshToken);

			resultVo.setSuccess(true);
			resultVo.setData(accessToken);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("刷新Oauth2AccessToken失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/getOauth2UserInfo")
	public void getOauth2UserInfo(HttpServletRequest request,
			HttpServletResponse response, String access_token, String openid) {
		ResultVo resultVo = new ResultVo();
		try {
			UserInfo userInfo = WeixinUtil.getOauth2UserInfo(access_token,
					openid);

			resultVo.setSuccess(true);
			resultVo.setData(userInfo);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("获取Oauth2用户信息失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/validateAccessToken")
	public void validateAccessToken(HttpServletRequest request,
			HttpServletResponse response, String access_token, String openid) {
		ResultVo resultVo = new ResultVo();
		try {
			boolean isAccessToken = WeixinUtil.validateAccessToken(
					access_token, openid);

			resultVo.setSuccess(true);
			resultVo.setData(isAccessToken);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("验证AccessToken失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/getOauth2UserInfoByCode")
	public void getOauth2UserInfoByCode(HttpServletRequest request,
			HttpServletResponse response,String appId, String appSecret,String code) {
		ResultVo resultVo = new ResultVo();
		try {
			if(appId == null || "".equals(appId)) appId = Constant.appId;
			if(appSecret == null || "".equals(appSecret)) appSecret = Constant.appSecret;
			AccessToken accessToken = WeixinUtil.getOauth2AccessToken(
					appId, appSecret, code);
			UserInfo userInfo = WeixinUtil.getOauth2UserInfo(
					accessToken.getAccess_token(), accessToken.getOpenid());
			resultVo.setSuccess(true);
			resultVo.setData(userInfo);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("根据CODE获取用户信息失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/getMedia")
	public void getMedia(HttpServletRequest request,
			HttpServletResponse response, String media_id) {
		ResultVo resultVo = new ResultVo();
		log.info("media_id:" + media_id);
		try {
			String str = WeixinUtil.getMedia(
					Constant.accessToken.getAccess_token(), media_id,
					CommonUtil.getRealPath(request) + FileUtil.fileRoot,
					"winxinImage");
			log.info(str);
			resultVo.setSuccess(true);
			resultVo.setData(str);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("getMedia失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	@RequestMapping(method = RequestMethod.POST, value = "/JSSDKSign")
	public void JSSDKSign(HttpServletRequest request,
			HttpServletResponse response, String url) {
		ResultVo resultVo = new ResultVo();
		try {
			SignVo signVo = SignUtil
					.sign(Constant.jsApiTicket.getTicket(), url);

			resultVo.setSuccess(true);
			resultVo.setData(signVo);
		} catch (NullPointerException e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("jsApiTicket 为空,JSSDKSign失败!");
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setSuccess(false);
			resultVo.setMessage("JSSDKSign失败!");
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	public static void main(String[] args) {
/*		AccessToken accessToken = WeixinUtil.getAccessToken(Constant.appId,
				Constant.appSecret);
		System.out.println(accessToken.getAccess_token());
		String media_id = "8S1BTo5QcguhHvNqWe8IUuqxyHJRCfTlyesxPbFa4pG-fcDp_sLil94egPkpUB-8";
		String str = WeixinUtil.getMedia(accessToken.getAccess_token(),
				media_id, "E:\\apache-tomcat-6.0.35\\webapps\\HRTenement\\"
						+ FileUtil.fileRoot, "/winxinImage/");
		System.out.println(str);*/

	}
}
